package com.webwork.mood.record.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.mood.record.dao.DrawDateDao;
import com.webwork.mood.record.dao.DrawInfoDao;
import com.webwork.mood.record.dao.DrawRecordDao;
import com.webwork.mood.record.dataobject.DrawInfo;
import com.webwork.mood.record.dataobject.DrawRecord;
import com.webwork.mood.record.service.DrawRecordService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Service
public class DrawRecordServiceImpl implements DrawRecordService {

	@Autowired
	private DrawRecordDao recordDao;
	
	@Autowired
	private DrawInfoDao drawDao;
	
	@Autowired
	private DrawDateDao drawDateDao;
	
	@Override
	public void insert(DrawRecord record) {
		recordDao.insert(record);
	}
	
	@Override
	public List<DrawRecord> query(DrawRecord record) {
		return this.query(record, new Paging(0, 0));
	}
	
	@Override
	public List<DrawRecord> query(DrawRecord record, Paging page) {
		return recordDao.query(record, page.getCurrPage(), page.getPageSize());
	}
	
	@Override
	public DrawRecord get(String userId, String awardName, Date drawDate) {
		if (StrManager.isEmpty(userId)) {
			return null;
		}
		DrawRecord queryObj = new DrawRecord();
		queryObj.setUserId(userId);
		queryObj.setAwardName(awardName);
		queryObj.setDrawDate(drawDate);
		List<DrawRecord> records = this.query(queryObj);
		if (records != null && records.size() > 0) {
			if (records.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return records.get(0);
			}
		} else {
			System.out.println("找不到该记录!");
		}
		return null;
	}

	@Override
	public int count(DrawRecord record) {
		return recordDao.count(record);
	}

	@Override
	public List<DrawInfo> query(DrawInfo draw) {
		return drawDao.query(draw, 0, 0);
	}

	@Override
	public List<DrawRecord> getUserDraw(String userId, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(date);
		return recordDao.getUserDraw(userId, dateStr);
	}
	
	@Override
	public int count(DrawInfo draw) {
		return drawDao.count(draw);
	}
	
	@Override
	public DrawInfo get(String userId, String awardName) {
		if (StrManager.isEmpty(userId)) {
			return null;
		}
		DrawInfo queryObj = new DrawInfo();
		queryObj.setUserId(userId);
		queryObj.setAwardName(awardName);
		List<DrawInfo> records = this.query(queryObj);
		if (records != null && records.size() > 0) {
			if (records.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return records.get(0);
			}
		} else {
			System.out.println("找不到该记录!");
		}
		return null;
	}
	
	@Override
	public void insert(DrawInfo drawInfo) {
		drawDao.insert(drawInfo);
	}

	@Override
	public void update(DrawInfo drawInfo) {
		drawDao.update(drawInfo);
	}

	@Override
	public List<Integer> getDrawDate() {
		List<Integer> dates = drawDateDao.query(0, 0, 0);
		
		Calendar a = Calendar.getInstance();
	    a.set(Calendar.DATE, 1);	//把日期设置为当月第一天 
	    a.roll(Calendar.DATE, -1);	//日期回滚一天，也就是最后一天
	    int maxDate = a.get(Calendar.DATE);
		for (Integer date : dates) {
			if (date > maxDate) {	//设置的日期如果为大于当月最大天数,即当作当月最后一天处理
				date = maxDate;
			}
		}
		return dates;
	}
	
}
